package com.siao.component
{
	import flash.display.DisplayObject;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	[Event(name="select", type="flash.events.Event")]
	
	/**
	 * 标签菜单组件
	 * @author SiaoLeon
	 */
	public class TabMenu extends Stack implements ISelector
	{
		// -------------------------------------------------------------------------------------------------------- 静态常量
		
		// -------------------------------------------------------------------------------------------------------- 静态变量
		
		// -------------------------------------------------------------------------------------------------------- 静态方法
		
		// -------------------------------------------------------------------------------------------------------- 静态函数
		
		// -------------------------------------------------------------------------------------------------------- 属性
		
		private var _selected:*;
		
		/**
		 * 选中的项目
		 * @return
		 */
		public function get selected():*
		{
			return this._selected;
		}
		
		public function set selected(value:*):void
		{
			this._selected = value;
			
			for (var i:int = 0; i < this.numChildren; i++)
			{
				var child:DisplayObject = this.getChildAt(i);
				
				if (child is ISelectItem)
				{
					(child as ISelectItem).selected = (value == child);
				}
			}
			
			this.dispatchEvent(new Event(Event.SELECT));
		}
		
		// -------------------------------------------------------------------------------------------------------- 变量
		
		// -------------------------------------------------------------------------------------------------------- 构造
		
		/**
		 * 构造函数
		 * @param orientation 排列方向
		 */
		public function TabMenu(orientation:uint=Orientation.LEFT_RIGHT)
		{
			super(orientation);
		}
		
		// -------------------------------------------------------------------------------------------------------- 方法
		
		override public function addChild(child:DisplayObject):DisplayObject
		{
			child.addEventListener(MouseEvent.CLICK, onClick, false, 0, true);
			
			return super.addChild(child);
		}
		
		override public function addChildAt(child:DisplayObject, index:int):DisplayObject
		{
			child.addEventListener(MouseEvent.CLICK, onClick, false, 0, true);
			
			return super.addChildAt(child, index);
		}
		
		override public function removeChild(child:DisplayObject):DisplayObject
		{
			child.removeEventListener(MouseEvent.CLICK, onClick);
			
			return super.removeChild(child);
		}
		
		override public function removeChildAt(index:int):DisplayObject
		{
			var child:DisplayObject = super.removeChildAt(index);
			
			child.removeEventListener(MouseEvent.CLICK, onClick);
			
			return child;
		}
		
		//		override public function removeChildren(beginIndex:int=0, endIndex:int=2147483647):void
		//		{
		//			for (var i:int = beginIndex; i < endIndex; i++)
		//			{
		//				var child:DisplayObject = this.getChildAt(i);
		//				child.removeEventListener(MouseEvent.CLICK, onClick);
		//			}
		//			
		//			super.removeChildren(beginIndex, endIndex);
		//			
		//			this.init();
		//		}
		
		// -------------------------------------------------------------------------------------------------------- 函数
		
		/**
		 * 鼠标点击时
		 * @param event
		 */
		private function onClick(event:MouseEvent):void
		{
			this.selected = event.currentTarget;
		}
	
	}
}
